package com.yehongyu.mytools.ui.awt;

//: BangBean.java
//A graphical Bean
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.Serializable;
import java.util.TooManyListenersException;

public class BangBean extends Canvas implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected int xm, ym;

	protected int cSize = 20; // Circle size

	protected String text = "Bang!";

	protected int fontSize = 48;

	protected Color tColor = Color.red;

	protected ActionListener actionListener;

	public BangBean() {
		addMouseListener(new ML());
		addMouseMotionListener(new MML());
	}

	public int getCircleSize() {
		return cSize;
	}

	public void setCircleSize(int newSize) {
		cSize = newSize;
	}

	public String getBangText() {
		return text;
	}

	public void setBangText(String newText) {
		text = newText;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int newSize) {
		fontSize = newSize;
	}

	public Color getTextColor() {
		return tColor;
	}

	public void setTextColor(Color newColor) {
		tColor = newColor;
	}

	public void paint(Graphics g) {
		g.setColor(Color.black);
		g.drawOval(xm - cSize / 2, ym - cSize / 2, cSize, cSize);
	}

	// This is a unicast listener, which is
	// the simplest form of listener management:
	public void addActionListener(ActionListener l)
			throws TooManyListenersException {
		if (actionListener != null)
			throw new TooManyListenersException();
		actionListener = l;
	}

	public void removeActionListener(ActionListener l) {
		actionListener = null;
	}

	class ML extends MouseAdapter {
		public void mousePressed(MouseEvent e) {
			Graphics g = getGraphics();
			g.setColor(tColor);
			g.setFont(new Font("TimesRoman", Font.BOLD, fontSize));
			int width = g.getFontMetrics().stringWidth(text);
			g.drawString(text, (getSize().width - width) / 2,
					getSize().height / 2);
			g.dispose();
			// Call the listener's method:
			if (actionListener != null)
				actionListener.actionPerformed(new ActionEvent(BangBean.this,
						ActionEvent.ACTION_PERFORMED, null));
		}
	}

	class MML extends MouseMotionAdapter {
		public void mouseMoved(MouseEvent e) {
			xm = e.getX();
			ym = e.getY();
			repaint();
		}
	}

	public Dimension getPreferredSize() {
		return new Dimension(200, 200);
	}

	// Testing the BangBean:
	public static void main(String[] args) {
		BangBean bb = new BangBean();
		try {
			bb.addActionListener(new BBL());
		} catch (TooManyListenersException e) {
		}
		Frame aFrame = new Frame("BangBean Test");
		aFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		aFrame.add(bb, BorderLayout.CENTER);
		aFrame.setSize(300, 300);
		aFrame.setVisible(true);
	}

	// During testing, send action information
	// to the console:
	static class BBL implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.out.println("BangBean action");
		}
	}
} // /:~
